/**
 * 
 */
package org.alipay4j.component.life;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayException;
import org.alipay4j.Configuration;
import org.alipay4j.component.AbstractComponent;
import org.alipay4j.http.AlipayHttpsClient;
import org.alipay4j.http.Response;
import org.alipay4j.model.label.LifeLabel;
import org.alipay4j.util.ObjectHelper;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author heliang
 * 标签操作组件
 */
public class LabelComponent extends AbstractComponent {

	public LabelComponent(Alipay alipay) {
		super(alipay);
	}
	
	
	/**
	 * 创建标签-开发者可使用该API创建自定义标签
	 * @param label_name 自定义标签名
	 * @param data_type 标签值类型，目前只支持string（字符串类型），不传默认为"string"
	 * @return label_id 标签id
	 * @throws AlipayException
	 */
	public String create(String label_name,String data_type) throws AlipayException {
		if (ObjectHelper.isEmpty(label_name)) {
            throw new IllegalArgumentException("label_name can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("label_name", label_name);
		body.put("data_type", data_type);
		body.put("method", "alipay.open.public.life.label.create");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_label_create_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_label_create_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return bodyRes.getString("label_id");
	}
	
	
	/**
	 * 标签修改接口
	 * @param label_id 标签id，调用创建标签接口后由支付宝返回 ，只支持生活号自定义标签
	 * @param label_name 标签名
	 * @return boolean
	 * @throws AlipayException
	 */
	public boolean modify(String label_id,String label_name) throws AlipayException {
		if (ObjectHelper.isEmpty(label_id)) {
            throw new IllegalArgumentException("label_id can't be null or empty");
        }
		if (ObjectHelper.isEmpty(label_name)) {
            throw new IllegalArgumentException("label_name can't be null or empty");
        }
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("label_id", label_id);
		body.put("label_name", label_name);
		body.put("method", "alipay.open.public.life.label.modify");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_label_modify_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_label_modify_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
		return true;
	}
	
	/**
	 * 标签批量查询接口
	 * @return List<LifeLabel>
	 * @throws AlipayException
	 */
	public List<LifeLabel> batchquery() throws AlipayException {
		//组装请求参数
		Map<String, String> body = new HashMap<String, String>();
		body.put("method", "alipay.open.public.life.label.batchquery");
		Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
		//根据请求结果判定，是否验证成功
		JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_label_batchquery_response");
    	if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_label_batchquery_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
        	String sub_code = bodyRes.getString("sub_code");
        	String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        List<LifeLabel> labels = new ArrayList<LifeLabel>();
        JSONArray array = bodyRes.getJSONArray("labels");
        for(int i=0;i<array.size();i++) {
        	LifeLabel label = JSONObject.toJavaObject(array.getJSONArray(i), LifeLabel.class);
        	labels.add(label);
        }
		return labels;
	}

	
	/**
	* @Title: 标签删除接口 
	* @Description: 开发者可调用此API删除自定义标签
	* @author heliang
	* @param label_id 标签id, 只支持生活号自定义标签
	* @return boolean
	* @throws AlipayException 
	 */
	public boolean delete(String label_id) throws AlipayException {
        if (ObjectHelper.isEmpty(label_id)) {
            throw new IllegalArgumentException("label_id can't be null or empty");
        }
        //组装请求参数
        Map<String, String> body = new HashMap<String, String>();
        body.put("label_id", label_id);
        body.put("method", "alipay.open.public.life.label.delete");
        Response response = new AlipayHttpsClient(super.alipay).postBizContent(body);
        //根据请求结果判定，是否验证成功
        JSONObject bodyRes = getObjRes(response, "alipay_open_public_life_label_delete_response");
        if (Configuration.isDebug()) {
            System.out.println("alipay_open_public_life_label_delete_response返回json：" + bodyRes.toString());
        }
        String codeRes = bodyRes.getString("code");
        if (ObjectHelper.isNotEmpty(codeRes)&&!"10000".equals(codeRes)) {
            String sub_code = bodyRes.getString("sub_code");
            String sub_msg = bodyRes.getString("sub_msg");
            //返回异常信息
            throw new AlipayException(getCause(sub_code,sub_msg));
        }
        return true;
    }
	
}
